package com.diqye.db.dialect;

import static com.diqye.templet.util.Strings.*;

import java.util.ArrayList;
import java.util.List;

import com.diqye.db.Tinfo;
import com.diqye.db.model.XModel;

public  class DialectBase implements IDialect {

	public String getdialectname(){
		return "base";
	}
	public String tableInfosql(String tablename){
		return "select * from "+tablename +" where 1=2";
	}
	
	public String savesql(XModel model){
		Tinfo tinfo = model.getTinfo();
		StringBuffer sbuf = new StringBuffer();
		sbuf.append("insert into ")
		.append(tinfo.getTablename())
		.append(" (");
		List<String> fields = new ArrayList<String>();
		fields.addAll(tinfo.getPrimaryKey());
		fields.addAll(model.notNullFields());
  		sbuf.append(join(fields,","));
		 sbuf.append(") ")
		.append("values (")
		.append(join(bachFormatfill("#{${0}}", fields),","))
		.append(")");
		return sbuf.toString();
	}
	
	public String deletesql(Tinfo tinfo){
		StringBuffer sbuf = new StringBuffer();
		sbuf.append("delete from ")
		.append(tinfo.getTablename())
		.append(" where ")
		.append(join(bachFormatfill("${0} = #{${0}}", tinfo.getPrimaryKey())," and "));
		return sbuf.toString();
	}
	public String deletemoresql(Tinfo tinfo,String where){
		StringBuffer sbuf = new StringBuffer();
		sbuf.append("delete from ")
		.append(tinfo.getTablename())
		.append(" where ")
		.append(where);
		return sbuf.toString();
	}
	public String updatesql(XModel model){
		Tinfo tinfo = model.getTinfo();
		List<String> fields = model.notNullFields();
		StringBuffer sbuf = new StringBuffer();
		sbuf.append("update ")
		.append(tinfo.getTablename())
		.append(" set ")
		.append(join(bachFormatfill("${0}=#{${0}}", fields),","))
		.append(" where ")
		.append(join(bachFormatfill("${0}=#{${0}}", tinfo.getPrimaryKey())," and "));
		return sbuf.toString();
	}
	
	public String updatemoresql(Tinfo tinfo,List<String> fields,String where){
		StringBuffer sbuf = new StringBuffer();
		sbuf.append("update ")
		.append(tinfo.getTablename())
		.append(" set ")
		.append(join(bachFormatfill("${0}=#{${0}}", fields),","))
		.append(" where ")
		.append(where);
		return sbuf.toString();
	}
	
	public String findbyidsql(Tinfo tinfo){
		StringBuffer sbuf = new StringBuffer();
		sbuf.append("select * from ")
		.append(tinfo.getTablename())
		.append(" where ")
		.append(join(bachFormatfill("${0}=#{${0}}", tinfo.getPrimaryKey())," and "));
		return sbuf.toString();
	}
}
